﻿using Data;
using Infrastructure.States;
using Services.Progress;
using Services.StaticData;
using UI;
using UnityEngine;

namespace Factory
{
    public class UiFactory : IUiFactory
    {
        private readonly IProgressService _progressService;
        private readonly GameStateMachine _stateMachine;
        private readonly IStaticDataService _staticDataService;
        private UiStaticData _uiData;
        private Transform _uiRoot;
        private HeroState _state;

        public UiFactory(IProgressService progressService,
            GameStateMachine stateMachine,
            IStaticDataService staticDataService)
        {
            _progressService = progressService;
            _stateMachine = stateMachine;
            _staticDataService = staticDataService;
        }

        public void CreateUiRoot()
        {
            _uiData = _staticDataService.Ui;
            _uiRoot = Object.Instantiate(_uiData.Root).transform;
            _state = _progressService.Progress.HeroState;
        }

        public GameObject CreateMenu()
        {
            var obj = Object.Instantiate(_uiData.Menu, _uiRoot);
            obj.TryGetComponent<MenuWindow>(out var window);
            window.Construct(_stateMachine);
            return obj;
        }

        public GameObject CreatePause()
        {
            var obj = Object.Instantiate(_uiData.Pause, _uiRoot);
            obj.TryGetComponent<PauseWindow>(out var window);
            window.Construct(_stateMachine);
            return obj;
        }

        public GameObject CreateEndGame()
        {
            var obj = Object.Instantiate(_uiData.EndGame, _uiRoot);
            obj.TryGetComponent<EndGameWindow>(out var window);
            window.Construct(_stateMachine, _state);
            return obj;
        }
    }
}